package cn.source.web.controller.zk;

import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.utils.NumberUtils;
import cn.source.zk.domain.entity.Channel;
import cn.source.zk.domain.entity.ConnectionPoint;
import cn.source.zk.service.IZkChannelService;
import cn.source.zk.service.IZkConnectionPointService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/zk/connectionPoint")
@Slf4j
public class ZkConnectionPointController extends BaseController {

    @Autowired
    private IZkConnectionPointService connectionPointService;

    @Autowired
    private IZkChannelService channelService;

    /**
     * 查询并网点分页列表
     * @param connectionPoint
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo listPage(ConnectionPoint connectionPoint){
        log.info("并网点信息：{}",connectionPoint);
        startPage();
        List<ConnectionPoint> list = connectionPointService.listPage(connectionPoint);
        return getDataTable(list);
    }

    /**
     * 新增并网点
     * @param connectionPoint
     * @return
     */
    @PostMapping()
    public AjaxResult add(@RequestBody ConnectionPoint connectionPoint){

        //校验变电站下是否有相同名称的并网点
        boolean result = connectionPointService.checkConnectionPointExist(connectionPoint.getConnectionPointName(), connectionPoint.getSubstationNum(),null);
        if (result){
            return AjaxResult.error("在当前变电站下存在同名并网点,请修改");
        }

        //设置创建人和修改人
        connectionPoint.setCreateBy(getUsername());
        connectionPoint.setUpdateBy(getUsername());
        // 设置容量位四舍五入；
        connectionPoint.setCapacity(NumberUtils.random3(connectionPoint.getCapacity()));

        return toAjax(connectionPointService.add(connectionPoint));
    }

    /**
     * 根据num查询并网点
     * @param num
     * @return
     */
    @GetMapping("/{num}")
    public AjaxResult getByNum(@PathVariable Long num){
        ConnectionPoint connectionPoint = connectionPointService.getByNum(num);
        return AjaxResult.success(connectionPoint);
    }

    /**
     * 修改并网点
     * @param connectionPoint
     * @return
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ConnectionPoint connectionPoint){
        //校验变电站下是否有相同名称的并网点
        boolean result = connectionPointService.checkConnectionPointExist(connectionPoint.getConnectionPointName(), connectionPoint.getSubstationNum(),connectionPoint.getNum());
        if (result){
            return AjaxResult.error("在当前变电站下存在同名并网点,请修改");
        }

        //设置修改人
        connectionPoint.setUpdateBy(getUsername());
        // 设置容量位四舍五入；
        connectionPoint.setCapacity(NumberUtils.random3(connectionPoint.getCapacity()));

        return toAjax(connectionPointService.edit(connectionPoint));
    }


    /**
     * 批量删除并网点
     * @param nums
     * @return
     */
    @DeleteMapping("/{nums}")
    public AjaxResult deleteBatch(@PathVariable List<Long> nums){

        //查询并网点下是否有通道
        List<Channel> channels = channelService.getByConnectionPointNums(nums);
        if (!channels.isEmpty()){
            return AjaxResult.error("并网点下有通道，不允许删除！");
        }

        return toAjax(connectionPointService.deleteBatch(nums));
    }

}
